package com.strawcom.charmring.view;

import org.kymjs.kjframe.utils.DensityUtils;

import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.Scroller;

import com.strawcom.charmring.R;

/**
 * 
 * @ClassName: MapFrameLayout
 * @Description:地图遮罩层自定义控件
 * @author liyongqiang
 * @date 2015-4-1 下午11:47:52
 * 
 */
public class MapFrameLayout extends FrameLayout implements OnTouchListener {

	/** 点击时候Y的坐标 */
	private int downY = 0;
	/** 拖动时候Y的坐标 */
	private int moveY = 0;
	/** 拖动时候Y的方向距离 */
	private int scrollY = 0;
	/** 松开时候Y的坐标 */
	private int upY = 0;
	/** 控件状态 */
	private boolean isMove = false;
	/** 控件默认向上偏移距离 */
	private int top;
	/** 上升动画时间 */
	private int upDuration = 1000;
	/** 下落动画时间 */
	private int downDuration = 500;
	private Scroller mScroller;
	private View mapframe;
	private Integer colorFrom;
	private Integer colorTo;
	private HeadHorizontalScrollView layoutHead;
	private boolean canTouch = true;

	/** 是否 打开 */
	private boolean isOpen = false;

	public MapFrameLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);

	}

	private void init(Context context) {
		mScroller = new Scroller(context);
		colorFrom = getResources().getColor(R.color.transparent);
		colorTo = getResources().getColor(R.color.transparent_66);
		mapframe = LayoutInflater.from(context).inflate(
				R.layout.layout_mapframe, null);
		layoutHead = (HeadHorizontalScrollView) mapframe
				.findViewById(R.id.layout_head);
		addView(mapframe);
		mapframe.post(new Runnable() {

			@Override
			public void run() {
				FrameLayout.LayoutParams lp = (LayoutParams) getLayoutParams();
				lp.height = DensityUtils.getScreenH(getContext())
						- DensityUtils.dip2px(getContext(), 120);
				setLayoutParams(lp);
				top = lp.height - DensityUtils.dip2px(getContext(), 80);
				MapFrameLayout.this.scrollTo(0, top);
			}
		});
		mapframe.setOnTouchListener(this);
	}
	
	public void setTouchFlag(boolean canTouch){
		this.canTouch=canTouch;
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent event) {
		if (canTouch) {
			if (!isMove) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					downY = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_MOVE:
					moveY = (int) event.getRawY();
					scrollY = moveY - downY;
					if (scrollY < 0) {
						// 向上滑动
						if (isOpen) {
							if (Math.abs(scrollY) <= top) {
								scrollTo(0, -scrollY);
							}
						}
					} else {
						// 向下滑动
						if (!isOpen) {
							if (scrollY <= top) {
								scrollTo(0, top - scrollY);
							}
						}
					}
					break;
				case MotionEvent.ACTION_UP:
					upY = (int) event.getRawY();
					if (Math.abs(upY - downY) < 10) {
						onRopeClick();
						break;
					}
					if (downY > upY) {
						// 向上滑动
						if (isOpen) {
							if (Math.abs(scrollY) > top / 8) {
								// 向上滑动1/8 top高的时候 开启向上消失动画
								startMoveAnim(this.getScrollY(),
										(top - this.getScrollY()), upDuration);
								isOpen = false;
							} else {
								startMoveAnim(this.getScrollY(),
										-this.getScrollY(), upDuration);
								isOpen = true;
							}
						}
					} else {
						// 向下滑动
						if (scrollY > top / 8) {
							// 向下滑动超过1/8 top高的时候 开启向下消失动画
							startMoveAnim(this.getScrollY(),
									-this.getScrollY(), upDuration);
							isOpen = true;
						} else {
							startMoveAnim(this.getScrollY(),
									(top - this.getScrollY()), upDuration);
							isOpen = false;
						}
					}
					break;
				default:
					break;
				}
			}
		}
		return true;
	}

	@Override
	public void computeScroll() {
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
			isMove = true;
		} else {
			isMove = false;
		}
		super.computeScroll();
	}

	/**
	 * 拖动动画
	 * 
	 * @param startY
	 * @param dy
	 *            垂直距离, 滚动的y距离
	 * @param duration
	 *            时间
	 */
	public void startMoveAnim(int startY, int dy, int duration) {
		isMove = true;
		mScroller.startScroll(0, startY, 0, dy, duration);
		if (dy > 0) {
			changeColor(colorTo, colorFrom, upDuration);
		} else {
			changeColor(colorFrom, colorTo, downDuration);
		}
		invalidate();
	}

	public void onRopeClick() {
		if (isOpen) {
			startMoveAnim(0, top, upDuration);
		} else {
			startMoveAnim(top, -top, downDuration);
		}
		isOpen = !isOpen;
	}

	public void changeColor(int colorFrom, int colorTo, long duration) {
		ValueAnimator colorAnimation = ValueAnimator.ofObject(
				new ArgbEvaluator(), colorFrom, colorTo);
		colorAnimation.setDuration(duration);
		colorAnimation.addUpdateListener(new AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animator) {
				layoutHead.setBackgroundColor((Integer) animator
						.getAnimatedValue());
			}
		});
		colorAnimation.start();
	}
}
